package checkers.gui.dialogs;

import checkers.model.Player;
import checkers.common.Cronometer;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;

/**
 * Class EndGameDialog
 * Display game end data
 * 
 * @author Cristian Tardivo
 */
public class EndGameDialog extends javax.swing.JDialog {
    private static final ResourceBundle lang = ResourceBundle.getBundle("checkers/common/lang");

   /**
    * Create new EndGameDialog
    * @param parent Parent Frame
    * @param modal Modal exclude
    * @param winner Winner Player
    * @param playerA Player A
    * @param playerB Player B
    */
    public EndGameDialog(java.awt.Frame parent, boolean modal,Player winner, Player playerA,Player playerB) {
        super(parent, modal);
        initComponents();
        // Winner data
        if(winner != null){
            winner_data.setText(winner.getName());        
            if(winner.isRed()){
                winner_data.setForeground(java.awt.Color.red);
            } else {
                winner_data.setForeground(java.awt.Color.black);
            }
        } else {
            winner_data.setText(lang.getString("GAME_DRAW"));
        }
        if(playerA == null) return;
        // Player A
        player1_name.setText(playerA.getName());
        player1_time.setText(Cronometer.getString(playerA.getPlayedTime()));
        player1_movements.setText(playerA.getMovementCount()+"");
        // Player B
        player2_name.setText(playerB.getName());
        
        player2_time.setText(Cronometer.getString(playerB.getPlayedTime()));
        player2_movements.setText(playerB.getMovementCount()+"");
        // PColor
        if(playerA.isRed()){
            player1_color.setText(lang.getString("RED"));
            player2_color.setText(lang.getString("BLACK"));
        } else {
            player1_color.setText(lang.getString("BLACK"));
            player2_color.setText(lang.getString("RED"));
        }
        this.getRootPane().setDefaultButton(close_butt);
        // Key ESCAPE action
        KeyStroke escapeKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false);
        Action escapeAction = new AbstractAction(){public void actionPerformed(ActionEvent e){close_butt.doClick();}}; 
        this.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(escapeKeyStroke, "ESCAPE");
        this.getRootPane().getActionMap().put("ESCAPE", escapeAction);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel12 = new javax.swing.JPanel();
        time_lbl = new javax.swing.JLabel();
        player1_color = new javax.swing.JLabel();
        name_lbl = new javax.swing.JLabel();
        color_lbl = new javax.swing.JLabel();
        player1_name = new javax.swing.JLabel();
        player1_time = new javax.swing.JLabel();
        movement_lbl = new javax.swing.JLabel();
        player1_movements = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        name_lbl2 = new javax.swing.JLabel();
        time_lbl2 = new javax.swing.JLabel();
        player2_time = new javax.swing.JLabel();
        player2_color = new javax.swing.JLabel();
        player2_name = new javax.swing.JLabel();
        color_lbl2 = new javax.swing.JLabel();
        movement_lbl2 = new javax.swing.JLabel();
        player2_movements = new javax.swing.JLabel();
        close_butt = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        winner_data = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(lang.getString("GAME_END")); // NOI18N
        setMinimumSize(new java.awt.Dimension(220, 355));
        setResizable(false);

        jPanel12.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("PLAYER_1"))); // NOI18N
        jPanel12.setMaximumSize(new java.awt.Dimension(189, 110));
        jPanel12.setMinimumSize(new java.awt.Dimension(189, 110));
        jPanel12.setPreferredSize(new java.awt.Dimension(189, 110));

        time_lbl.setText(lang.getString("PLAYED_TIME")); // NOI18N

        player1_color.setText("                              "); // NOI18N

        name_lbl.setText(lang.getString("NAME")); // NOI18N

        color_lbl.setText(lang.getString("COLOR")); // NOI18N

        player1_name.setText("                              "); // NOI18N

        player1_time.setText("                              "); // NOI18N

        movement_lbl.setText(lang.getString("MOVEMENTS")); // NOI18N

        player1_movements.setText("                              "); // NOI18N

        javax.swing.GroupLayout jPanel12Layout = new javax.swing.GroupLayout(jPanel12);
        jPanel12.setLayout(jPanel12Layout);
        jPanel12Layout.setHorizontalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(movement_lbl)
                    .addComponent(time_lbl)
                    .addComponent(color_lbl)
                    .addComponent(name_lbl))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(player1_name)
                    .addComponent(player1_color)
                    .addComponent(player1_time)
                    .addComponent(player1_movements))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel12Layout.setVerticalGroup(
            jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel12Layout.createSequentialGroup()
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name_lbl)
                    .addComponent(player1_name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(color_lbl)
                    .addComponent(player1_color))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(time_lbl)
                    .addComponent(player1_time))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addGroup(jPanel12Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(movement_lbl)
                    .addComponent(player1_movements))
                .addContainerGap())
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("PLAYER_2"))); // NOI18N
        jPanel1.setMaximumSize(new java.awt.Dimension(189, 110));
        jPanel1.setMinimumSize(new java.awt.Dimension(189, 110));
        jPanel1.setPreferredSize(new java.awt.Dimension(189, 110));

        name_lbl2.setText(lang.getString("NAME")); // NOI18N

        time_lbl2.setText(lang.getString("PLAYED_TIME")); // NOI18N

        player2_time.setText("                              "); // NOI18N

        player2_color.setText("                              "); // NOI18N

        player2_name.setText("                              "); // NOI18N

        color_lbl2.setText(lang.getString("COLOR")); // NOI18N

        movement_lbl2.setText(lang.getString("MOVEMENTS")); // NOI18N

        player2_movements.setText("                              "); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(time_lbl2)
                    .addComponent(color_lbl2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(name_lbl2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(movement_lbl2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(player2_movements)
                    .addComponent(player2_time)
                    .addComponent(player2_color)
                    .addComponent(player2_name))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name_lbl2)
                    .addComponent(player2_name))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(color_lbl2)
                    .addComponent(player2_color))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(time_lbl2)
                    .addComponent(player2_time))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(movement_lbl2)
                    .addComponent(player2_movements))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        close_butt.setText(lang.getString("CLOSE")); // NOI18N
        close_butt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_buttActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(lang.getString("WINNER"))); // NOI18N
        jPanel2.setMaximumSize(new java.awt.Dimension(189, 50));
        jPanel2.setMinimumSize(new java.awt.Dimension(189, 50));
        jPanel2.setPreferredSize(new java.awt.Dimension(189, 50));

        winner_data.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        winner_data.setText("winner data");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(47, Short.MAX_VALUE)
                .addComponent(winner_data)
                .addContainerGap(47, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(winner_data)
                .addGap(0, 7, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(close_butt)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel12, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jPanel12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(close_butt)
                .addGap(6, 6, 6))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void close_buttActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_buttActionPerformed
        dispose();
    }//GEN-LAST:event_close_buttActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton close_butt;
    private javax.swing.JLabel color_lbl;
    private javax.swing.JLabel color_lbl2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel12;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel movement_lbl;
    private javax.swing.JLabel movement_lbl2;
    private javax.swing.JLabel name_lbl;
    private javax.swing.JLabel name_lbl2;
    public javax.swing.JLabel player1_color;
    public javax.swing.JLabel player1_movements;
    public javax.swing.JLabel player1_name;
    public javax.swing.JLabel player1_time;
    public javax.swing.JLabel player2_color;
    public javax.swing.JLabel player2_movements;
    public javax.swing.JLabel player2_name;
    public javax.swing.JLabel player2_time;
    private javax.swing.JLabel time_lbl;
    private javax.swing.JLabel time_lbl2;
    private javax.swing.JLabel winner_data;
    // End of variables declaration//GEN-END:variables
}
